CREATE TABLE [dbo].[tb_Inscripciones]
(
[idInscripcion] [int] NOT NULL IDENTITY(1, 1),
[Alumno] [int] NOT NULL,
[AnioLectivo] [int] NOT NULL,
[Organizacion] [int] NOT NULL,
[Carrera] [int] NOT NULL,
[Curso] [varchar] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Escolarizado] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Resuelta] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[U_Educativa] [varchar] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[AbanderadoEscolta] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[TieneHermanos] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PadresEnLaInstitucion] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Turno] [int] NULL,
[Baja] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_tb_Inscripciones_Baja] DEFAULT ('N'),
[CausaBaja] [varchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[EtapaInscripcion] [int] NULL,
[cargo] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_tb_Inscripciones_cargo] DEFAULT (suser_sname()),
[FechaCarga] [datetime] NULL,
[Nivel] [int] NULL,
[OrganiazacionOrigen] [int] NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[InscripcionesrepetidasPolimdal] ON [dbo].[tb_Inscripciones] 
FOR INSERT
AS

SET NOCOUNT ON
DECLARE @idalumno INT
DECLARE @AnioLectivo INT
DECLARE @carrera INT
DECLARE @idorganizacion INT
SELECT  @idalumno = alumno,@AnioLectivo = AnioLectivo, @carrera = carrera, @idorganizacion = Organizacion FROM Inserted 
if  (Select count(*) from tb_Inscripciones where Alumno = @idalumno and AnioLectivo = @AnioLectivo and carrera = @carrera and Organizacion = @idorganizacion ) >1 begin
	ROLLBACK transaction 
	RAISERROR('El Alumno  ya ha sido inscripto.', 16,1)
          
END
GO
ALTER TABLE [dbo].[tb_Inscripciones] ADD CONSTRAINT [PK_tb_Inscripciones] PRIMARY KEY NONCLUSTERED  ([idInscripcion]) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [idx_Alumno] ON [dbo].[tb_Inscripciones] ([Alumno]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_Inscripciones] WITH NOCHECK ADD CONSTRAINT [FK_tb_Inscripciones_tb_Alumnos] FOREIGN KEY ([Alumno]) REFERENCES [dbo].[tb_Alumnos] ([idAlumno])
GO
ALTER TABLE [dbo].[tb_Inscripciones] WITH NOCHECK ADD CONSTRAINT [FK_tb_Inscripciones_tb_GrupoDeCarreras] FOREIGN KEY ([Carrera]) REFERENCES [dbo].[tb_GrupoDeCarreras] ([idGrupoCarrera])
GO
ALTER TABLE [dbo].[tb_Inscripciones] WITH NOCHECK ADD CONSTRAINT [FK_tb_Inscripciones_tb_NivelesEnsenanza] FOREIGN KEY ([Nivel]) REFERENCES [dbo].[tb_NivelesEnsenanza] ([idNivelEnsenanza]) NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[tb_Inscripciones] WITH NOCHECK ADD CONSTRAINT [FK_tb_Inscripciones_tb_Organizaciones1] FOREIGN KEY ([OrganiazacionOrigen]) REFERENCES [dbo].[tb_Organizaciones] ([idOrganizaciones]) NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[tb_Inscripciones] WITH NOCHECK ADD CONSTRAINT [FK_tb_Inscripciones_tb_Organizaciones] FOREIGN KEY ([Organizacion]) REFERENCES [dbo].[tb_Organizaciones] ([idOrganizaciones])
GO
ALTER TABLE [dbo].[tb_Inscripciones] WITH NOCHECK ADD CONSTRAINT [FK_tb_Inscripciones_tb_Turnos] FOREIGN KEY ([Turno]) REFERENCES [dbo].[tb_Turnos] ([idTurno])
GO
ALTER TABLE [dbo].[tb_Inscripciones] NOCHECK CONSTRAINT [FK_tb_Inscripciones_tb_NivelesEnsenanza]
GO
ALTER TABLE [dbo].[tb_Inscripciones] NOCHECK CONSTRAINT [FK_tb_Inscripciones_tb_Organizaciones1]
GO
GRANT SELECT ON  [dbo].[tb_Inscripciones] TO [Coordinaciones]
GRANT INSERT ON  [dbo].[tb_Inscripciones] TO [Coordinaciones]
GRANT UPDATE ON  [dbo].[tb_Inscripciones] TO [Coordinaciones]
GO
